<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php学习笔记[010]</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="show"><span>020 PHP面向对象开发学习（四）</span></div>
<?php include 'include/top.php';?>

<div class="text"><b>继承关键字extends</b>
<strong>extends</strong>用于继承基类时使用，使用语法<br />
class SonClassName <strong>extends</strong> RootClassName {...}<br />

</div>
<div class="text"><b>继承规则</b>
遵循单一继承规则，一个子类有且仅有一个父类。子类拥有父类所有的属性及方法。应避免子类属性与方法与父类同名。<br />
</div>
<div class="text"><b>基类方法访问和重载</b>
有时候子类需要用与父类相同的方法且仅需要在父类中做小改动。可以使用重载的方式。可减少方法名的使用。<br />
我们用一个实例来说明上面的三个知识点<br />
<div class="code">
  &lt;?php<br />
    class Root<br />
    {<br />
    function prt1()<br />
    {<br />
    return 'this is print 1 in class Root!&lt;br /&gt;';<br />
    }<br />
    function vod1()<br />
    {<br />
    return 'this is vod 1 in class Root!&lt;br /&gt;';<br />
    }</p>
  <p>}</p>
  <p>class Son extends Root //继承<br />
    {<br />
    function prt2()<br />
    {<br />
    return 'this is print 2 in class Root!&lt;br /&gt;';<br />
    }<br />
    function vod1()<br />
    {<br />
    return Root::vod1().'this is vod 2 in class Root!&lt;br /&gt;';//重载<br />
    }<br />
    }</p>
  <p>$p=new Son();<br />
    echo $p-&gt;prt1();<br />
    echo $p-&gt;vod1();<br />
    ?&gt;<br />
</div>
其效果如下:
<div class="effect">
<?php
class Root
{
function prt1()
	{
	return 'this is print 1 in class Root!<br />';
	}
function vod1()
	{
	return 'this is vod 1 in class Root!<br />';
	}

}

class Son extends Root //继承
{
function prt2()
	{
	return 'this is print 2 in class Root!<br />';
	}
function vod1()
	{
	return Root::vod1().'this is vod 2 in class Root!<br />';//重载
	}
}

$p=new Son();
echo $p->prt1();
echo $p->vod1();
?>
</div>
</div>

</body>
</html>
